package com.fun_ordering.config;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.refund.RefundService;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@ConfigurationProperties(prefix = "wechat.pay")
public class WechatPayProperties {
    private String appId; // 小程序的appid
    private String appSecret; // 小程序的密钥
    private String mchId; //商户id
    private String mchSerialNumber; //商户序列号
    private String apiV3Key; // apiV3Key
    private String notifyUrl; //回调通知URL地址 必须是https协议
    private String privateKeyPath; //商户私钥路径

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getMchSerialNumber() {
        return mchSerialNumber;
    }

    public void setMchSerialNumber(String mchSerialNumber) {
        this.mchSerialNumber = mchSerialNumber;
    }

    public String getApiV3Key() {
        return apiV3Key;
    }

    public void setApiV3Key(String apiV3Key) {
        this.apiV3Key = apiV3Key;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getPrivateKeyPath() {
        return privateKeyPath;
    }

    public void setPrivateKeyPath(String privateKeyPath) {
        this.privateKeyPath = privateKeyPath;
    }

    @Bean
    public Config config() {
            return new RSAAutoCertificateConfig.Builder()  //使用自动更新平台证书的RSA配置   RSAConfig.Builder()使用本地
                    .merchantId(mchId)
                    .merchantSerialNumber(mchSerialNumber)
                    .apiV3Key(apiV3Key)
                    .privateKeyFromPath(privateKeyPath)
                    .build();
    }

    //构建Jsapi service 实例

    @Bean
    public JsapiService jsapiService() {
        return new JsapiService.Builder().config(config()).build();
    }

    @Bean
    public RefundService refundService(){   //SDK的退款实现类
        return new RefundService.Builder().config(config()).build();
    }

    @Bean
    public JsapiServiceExtension serviceExtension() {   //SDK的jsapi实现类扩展 帮我们完成了签名等其他步骤
        return new JsapiServiceExtension
                .Builder()
                .signType("RSA")
                .config(config())
                .build();
    }
}
